# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: parse_txt.py
  @time: 2022/4/14 14:11
  @desc:
"""

import numpy as np
import pandas as pd
from collections import namedtuple

fn = "D:\PyCharmProject\HC_ATS\static\SubDie_555.txt"

left = ["H1", "H2", "P2", "P1"]
right = ["P1", "P2", "H2", "H1"]


def parse_txt_to_dict(fn=fn):
    meta_data = np.loadtxt(fn, dtype=str, delimiter=",")
    final_data = meta_data[1:]
    result = {}
    for value in final_data:
        result[value[0].strip(",")] = [i.strip(",").strip() for i in value[1:]]
    if result:
        return result


def __func(listTemp, n):
    for i in range(0, len(listTemp), n):
        yield listTemp[i:i + n]


def judge_moving_direction(current_subdie, subdie_info):
    # H1左H2南P1右P2北
    if current_subdie in subdie_info:
        list_keys = list(subdie_info.keys())
        subdie_num_list = [i for i in range(len(list_keys))]
        # print("subdie_num_list",subdie_num_list)
        current_index = list_keys.index(current_subdie)
        next_index = 0 if current_index == len(list_keys) - 1 else current_index + 1
        # print("next_index",next_index)
        current_array = np.array(subdie_info[list_keys[current_index]], dtype=np.float32)
        # print("current_array",current_array)
        next_array = np.array(subdie_info[list_keys[next_index]], dtype=np.float32)
        # print("next_array",next_array)
        if current_index < len(list_keys) - 1:
            final_subdie_label = list_keys[current_index + 1]
            final_subdie_num = subdie_num_list[current_index + 1]
        else:
            final_subdie_label = list_keys[0]
            final_subdie_num = subdie_num_list[0]
        final_array = next_array - current_array
        print("before",final_array)
        # final_array[-4] = -final_array[-4]
        # final_array[-2] = -final_array[-2]
        # print("after",final_array)
        result = __func(final_array, 2)
        result_list = [i.tolist() for i in list(result)]
        # print(result_list)
        moveTuple = namedtuple('moveTuple', 'subdie_label, subdie_num,left_list,right_list')
        data_order = ["H1", "H2", "P1", "P2"]
        left_order = []
        right_order = []
        for i in range(len(result_list)):
            print("result_list[i][0]", result_list[i][0])
            if result_list[i][0] > 0:
                print("向右移动，最右边的探针先移动")
                right_order.append(data_order[i])
            else:
                print("向左移动，最左边的探针先移动")
                left_order.append(data_order[i])

        final_left = [i for i in left for j in left_order if left_order and i == j]
        final_right = [i for i in right for j in right_order if right_order and i == j]
        return moveTuple(final_subdie_label, final_subdie_num, final_left, final_right)


#
# result = parse_txt_to_dict()
# # print(result)
# current = "SubDie2"
# move_result = judge_moving_direction(current, result)
# print(move_result)
